<!-- 菜单管理 -->
<template>
  <div>
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-button
          type="primary"
          @click="(dialogVisible = true), (showDisable = false)"
          >新增</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      stripe
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="name" label="名称" sortable width="180">
      </el-table-column>
      <el-table-column prop="perms" label="权限编码" sortable width="180">
      </el-table-column>
      <el-table-column prop="icon" label="图标"> </el-table-column>
      <el-table-column prop="type" label="类型">
        <!-- 自定义插槽  后期需要动态生成 -->
        <template slot-scope="scope">
          <el-tag size="small" v-if="scope.row.type === 0">目录</el-tag>
          <el-tag size="small" type="success" v-else-if="scope.row.type === 1"
            >菜单</el-tag
          >
          <el-tag size="small" type="info" v-else-if="scope.row.type === 2"
            >按钮</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column prop="path" label="菜单URL"> </el-table-column>
      <el-table-column prop="component" label="菜单组件"> </el-table-column>
      <el-table-column prop="orderNum" label="排序号"> </el-table-column>
      <el-table-column prop="statu" label="状态">
        <template slot-scope="scope">
          <el-tag type="success" size="small" v-if="scope.row.statu === 1"
            >正常</el-tag
          >
          <el-tag type="danger" size="small" v-if="scope.row.statu === 0"
            >禁用</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column prop="icon" label="操作">
        <template slot-scope="scope">
          <el-button @click="editHandle(scope.row)" type="text" size="small"
            >编辑</el-button
          >
          <el-divider direction="vertical"></el-divider>
          <!-- <el-button type="text" size="small">删除</el-button> -->
          <el-button type="text" @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增对话框 -->
    <el-dialog
      title="新增菜单"
      :visible.sync="dialogVisible"
      width="650px"
      :before-close="handleClose"
    >
      <!-- 弹窗表单 -->
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editForm"
        label-width="100px"
        class="demo-editForm"
      >
        <el-form-item label="上级菜单" prop="parentId">
          <el-select
            v-model="editForm.parentId"
            placeholder="请选择上级菜单"
            :disabled="showDisable"
            clearable
          >
            <!-- 遍历生成的菜单列表进行填充 -->
            <template v-for="item in tableData">
              <el-option
                :label="item.name"
                :value="item.id"
                :key="item.id"
              ></el-option>
              <!-- 二级菜单 -->
              <template v-for="child in item.children">
                <el-option
                  :label="child.name"
                  :value="child.id"
                  :key="child.id"
                >
                  <span>{{ "- " + child.name }}</span>
                </el-option>
              </template>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单名称" prop="name" label-width="100px">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限编码" prop="perms" label-width="100px">
          <el-input v-model="editForm.perms" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标" prop="icon" label-width="100px">
          <el-input v-model="editForm.icon" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单URL" prop="path" label-width="100px">
          <el-input v-model="editForm.path" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单组件" prop="component" label-width="100px">
          <el-input v-model="editForm.component" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="type" label-width="100px">
          <el-radio-group v-model="editForm.type">
            <el-radio :label="0">目录</el-radio>
            <el-radio :label="1">菜单</el-radio>
            <el-radio :label="2">按钮</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态" prop="statu" label-width="100px">
          <el-radio-group v-model="editForm.statu">
            <el-radio :label="0">禁用</el-radio>
            <el-radio :label="1">正常</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序号" prop="orderNum" label-width="100px">
          <el-input-number v-model="editForm.orderNum" :min="1" label="排序号"
            >1</el-input-number
          >
        </el-form-item>
        <el-form-item>
          <el-button @click="resetForm('editForm')">取消</el-button>
          <el-button type="primary" @click="submitForm('editForm')"
            >确定</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
//这里可以导入其他文件(比如:组件、工具js、)
//例如：import 组件名 from '组件路径'
export default {
  //import引入的组件需要注入到对象中才能使用
  data() {
    return {
      dialogVisible: false,
      showDisable: false,
      editForm: {
        name: "",
        perms: "",
        icon: "",
        path: "",
        component: "",
        type: "",
        statu: "",
        orderNum: "",
        parentId: ""
      },
      editFormRules: {
        parentId: [
          { required: false, message: "请选择上级菜单", trigger: "blur" }
        ],
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        perms: [{ required: true, message: "请输入权限编码", trigger: "blur" }],
        type: [{ required: true, message: "请选择状态", trigger: "blur" }],
        orderNum: [
          { required: true, message: "请填写排序号", trigger: "blur" }
        ],
        statu: [{ required: true, message: "请输入名称", trigger: "blur" }]
      },
      tableData: []
    };
  },

  components: {},

  computed: {},

  // mounted: {},

  methods: {
    editHandle(row) {
      console.log(row);
      //是否是一级菜单 如何是一级菜单 直接禁用
      if (row.parentId == 0) {
        this.showDisable = true;
      } else {
        this.showDisable = false;
      }
      let id = row.id;
      this.$http.get("/sys/menu/info/" + id).then(res => {
        //数据封装
        this.editForm = res.data.data;
        //回显编辑菜单
        this.dialogVisible = true;
      });
    },
    onSubmit() {
      this.dialogVisible = true;
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          done();
          this.editForm = {};
          //表单重置
          this.$refs["editForm"].resetFields();
        })
        .catch(_ => {});
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$http
            .post(
              "/sys/menu/" + (this.editForm.id ? "update" : "save"),
              this.editForm
            )
            .then(res => {
              this.$message({
                showcClose: true,
                type: "success",
                message: "提交成功!",
                onClose: () => {
                  //更新成功后 刷新菜单列表
                  this.getMenuTree();
                }
              });
              this.$refs[formName].resetFields();
              this.dialogVisible = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false;
    },
    deleteHandle(id) {
      this.$confirm("此操作将删除该菜单, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$http.post("/sys/menu/delete/" + id).then(res => {
            this.$message({
              type: "success",
              message: "删除成功!",
              onClose: () => {
                //更新成功后 刷新菜单列表
                this.getMenuTree();
              }
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    getMenuTree() {
      this.$http.get("/sys/menu/list").then(res => {
        this.tableData = res.data.data;
      });
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getMenuTree();
  }
};
</script>
<style scoped>
/* .menu{
    display: flex;
    justify-content: flex-start;
  } */
</style>
